// pages/zhishi/zhishi.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showCommentArea: false,
    commentList: [],  // 评论列表
    inputValue: '',   // 输入框的值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var detail = wx.getStorageSync('detail');
    var user = wx.getStorageSync('user');
    this.setData({
      data: detail,
      user: user
    })
    wx.request({
      url: 'http://localhost:9000/comment',
      method: "GET",
      success:(res)=>{
        this.setData({
          commentList: res.data
        })
      }
    })
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let detail=wx.getStorageSync('detail')
    　　wx.setNavigationBarTitle({
    　　　　title: detail.type     //页面切换，更换页面标题
    　　})
  },


    //收藏功能
    giveAdd:function(){

      //console.log(this.data.data);
      var res=this.data.data
      if(res.is_add){   
        res.add--
        res.is_add=false           
      }else{      
        res.add++
        res.is_add=true
      }

      var data=this.data.data.id
      var url='http://localhost:9000/article/selectById/'+ data

    
      //通过id查询
      wx.request({
        url: 'http://localhost:9000/article',
        method:'POST',
        data: res,
        success:()=>{
          //console.log(res);
          wx.request({
            url: url,
            method: 'GET',
            success: (result) => {             
              this.setData({           
                data: result.data
              })        
            },
          })
        }
      })
    },

    giveCollect: function(){
      var res=this.data.data
      if(res.is_collect){
        res.is_collect=false
        res.collect--
        wx.showToast({
          title: '已取消收藏',
          duration: 1500
        })
      }else{
        res.is_collect=true
        res.collect++
        wx.showToast({
          title: '收藏成功',
          duration: 1500
        })
      } 
      var data=this.data.data.id
      var url='http://localhost:9000/article/selectById/'+ data

    
      //通过id查询
      wx.request({
        url: 'http://localhost:9000/article',
        method:'POST',
        data: res,
        success:()=>{
          wx.request({
            url: url,
            method: 'GET',
            success: (result) => {
              //wx.hideToast()
              this.setData({           
                data: result.data
              })        
            },
          })
        }
      })
    },

    showComment: function () {
      //console.log(this.data);
      this.setData({
        showCommentArea: true
      })
    },
    closeComment: function(){
      this.setData({
        showCommentArea: false
      })
    },
  

  // 输入框输入事件
  onInput(event) {
    this.setData({
      inputValue: event.detail.value,
    });
  },

  commentAdd:function(e){
    var res=this.data.commentList[e.currentTarget.id-1]
    //console.log(res);
      if(res.is_add){   
        res.add--
        res.is_add=false           
      }else{      
        res.add++
        res.is_add=true
      }

      //更新点赞数据
      wx.request({
        url: 'http://localhost:9000/comment',
        method:'POST',
        data: res,
        success:()=>{
          //console.log(res);
          wx.request({
            url: 'http://localhost:9000/comment',
            method: 'GET',
            success: (result) => {             
              this.setData({           
                commentList: result.data
              })        
            },
          })
        }
      })


  },
  // 提交按钮点击事件
  onSubmit() {
    var length=this.data.commentList.length
    // 获取当前时间
    const now = new Date();
    const time = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;

    // 将新评论添加到评论列表中
    const newComment = {
      id: length+1,
      avatar: this.data.user.imgUrl,
      nickname: this.data.user.nickname,
      content: this.data.inputValue,
      is_add: false,
      add: 0,
      createTime: time,
    };
    const commentList = this.data.commentList.concat(newComment);

    // 更新数据
    this.setData({
      commentList,
      inputValue: '',
    });

    //插入评论
    wx.request({
      url: 'http://localhost:9000/comment',
      method: "PUT",
      data: newComment,
      success:()=>{   
        wx.showToast({
          title: '发布成功',
        })
      
      }
    })
    //评论数加1
    this.data.data.comment++;
    this.setData({
      data: this.data.data
    })
    //更新文章评论数
    wx.request({
      url: 'http://localhost:9000/article',
      method:'POST',
      data: this.data.data,
    })

  },
  

})